// pages/information/information.js
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import api from '../../utils/api'
import dayjs from '../../miniprogram_npm/dayjs/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageBaseURL: getApp().imageBaseURL,
    imageResizeParams: getApp().imageResizeParams,
    swiper_banner: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    option1: [
      { text: '最新发布', value: 0 },
      { text: '浏览量', value: 1 },
      { text: '收藏量', value: 2 },
    ],
    timeOption: [
      { text: '全部', value: 'a' },
      { text: '今天', value: 'b' },
      { text: '近七天', value: 'c' },
      { text: '近一个月', value: 'd' },
      { text: '自定义选择时间', value: 'e' },
    ],
    banner: [],
    noData: false,
    search: '',
    value1: 0,
    time: 'a',
    scrollTop: 0,
    list:[],
    startTime: '',
    endTime: '',
    timeShow: false,
    minDate: new Date(2020, 7, 1).getTime(),
    maxDate: new Date().getTime(),
    defaultDate: [new Date().getTime() - 1000 * 60 * 60 * 24, new Date().getTime()],
    offset: 1,
    limit: 10
  },
  sortChange(e) {
    this.setData({ list: [], noData: false, offset: 1, value1: e.detail})
    this.getList()
  },
  timeChange(e) {
    const value =  e.detail
    if(value === 'a') {
      this.setData({time: value, startTime: '', endTime: ''})
    }else if(value === 'b') {
      this.setData({time: value, startTime: dayjs().format('YYYY-MM-DD'), endTime: dayjs().format('YYYY-MM-DD')})
    }else if(value === 'c') {
      this.setData({time: value, startTime: dayjs().subtract(6, 'days').format('YYYY-MM-DD'), endTime: dayjs().format('YYYY-MM-DD')})
    }else if(value === 'd') {
      this.setData({time: value, startTime: dayjs().subtract(29, 'days').format('YYYY-MM-DD'), endTime: dayjs().format('YYYY-MM-DD')})
    }else if(value === 'e') {
      this.setData({timeShow: true, time: value})
    }
    this.setData({ list: [], noData: false, offset: 1 })
    this.getList()
  },
  onConfirm(e) {
    const times = e.detail
    let { startTime, endTime, timeOption } = this.data
    startTime = dayjs(times[0]).format('YYYY-MM-DD')
    endTime = dayjs(times[1]).format('YYYY-MM-DD')
    timeOption[timeOption.length - 1].text = startTime + ' 至 ' + endTime
    this.setData({timeShow: false, startTime, endTime,timeOption})
    this.setData({ list: [], noData: false, offset: 1 })
    this.getList()

  },
  onClose () {
    this.setData({timeShow: false})
  },
  closed (e) {
    if(this.data.time === 'e') {
      this.setData({timeShow: true})
    }
  },
  async getBanner() {
    const params = {
      flag: 'auth/getAuthNewsBanner'
    }
    const { code, data, msg }  = await api.send(params) 
    if(code === 1) {
      data.forEach(item => {
        if(!item.banner_src.startsWith('http')) {
          item.banner_src = this.data.imageBaseURL +　item.banner_src + this.data.imageResizeParams
        }
      })
      this.setData({swiper_banner: data})
    }else {
      Toast(msg)
    }
  },
  async getList() {
    const { search,value1, startTime, endTime, offset, limit } = this.data
    const params = {
      flag: '/news/newsList',
      search: search,
      sort: value1,
      start_time: startTime,
      end_time: endTime,
      order: 'desc',
      offset,
      limit
    }
    const { code, data, msg }  = await api.send(params) 
    if(code === 1) {
      let { noData, limit, list, offset } = this.data
      if(data.rows.length < limit) {
        noData = true
      }else {
        noData = false
        offset += 1
      }
      data.rows.forEach(item => {
        if(!item.image.startsWith('http')) {
          item.image = this.data.imageBaseURL +　item.image + this.data.imageResizeParams
          item.publish_time = dayjs(item.publish_time * 1000).format('YYYY-MM-DD')
        }
      })
      list.push(...data.rows)
      this.setData({list, noData, offset})
    }else {
      Toast(msg)
    }
  },
  onSearch () {
    this.setData({ list: [], noData: false, offset: 1 })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBanner()
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onPageScroll: function(e) {
    // 页面滚动时执行
    this.setData({scrollTop: e.scrollTop})
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { noData } = this.data
    if( !noData)  {
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})